package com.tanhua.server.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * 通用设置业务层
 */
@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;

    /**
     * 接口名称：用户通用设置 - 读取
     */
    public ResponseEntity<Object> querySettings() {
        // 1.获取登录用户信息
        User user = UserHolder.get();

        // 2.查询用户的陌生人问题
        Question question = questionApi.findByUserId(user.getId());

        // 3.查询用户的通知设置信息
        Settings settings = settingsApi.findByUserId(user.getId());

        // 4.封装返回结果
        SettingsVo vo = new SettingsVo();
        // 4.1 对象拷贝
        if (settings != null) {
            BeanUtils.copyProperties(settings, vo);
        }
        // 4.2 设置手机号码
        vo.setPhone(user.getMobile());

        // 4.3 设置陌生人问题
        if (question != null) {
            vo.setStrangerQuestion(question.getTxt());
        } else {
            vo.setStrangerQuestion("你爱我吗");
        }

        return ResponseEntity.ok(vo);
    }

    /**
     * 接口名称：通知设置 - 保存
     */
    public ResponseEntity<Object> saveNotification(Settings param) {
        // 1.获取当前登录用户id
        Long userId = UserHolder.getUserId();

        // 2.查询用户的通用设置
        Settings settings = settingsApi.findByUserId(userId);

        if (settings == null) {
            // 新增用户的通知设置
            settings = new Settings();
            BeanUtils.copyProperties(param, settings);
            settings.setUserId(userId);

            settingsApi.save(settings);

        } else {
            // 修改用户的通知设置
            settings.setPinglunNotification(param.getPinglunNotification());
            settings.setGonggaoNotification(param.getGonggaoNotification());
            settings.setLikeNotification(param.getLikeNotification());

            settingsApi.update(settings);
        }

        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：设置陌生人问题 - 保存
     */
    public ResponseEntity<Object> saveQuestion(String content) {
        // 1.获取当前登录用户id
        Long userId = UserHolder.getUserId();

        // 2.查询用户的陌生人问题
        Question question = questionApi.findByUserId(userId);

        if(question == null){
            // 如果陌生人问题不存在就新增
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else{
            // 如果陌生人问题存在就更新
            question.setTxt(content);
            questionApi.update(question);
        }

        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：黑名单 - 翻页列表
     */
    public ResponseEntity<Object> blacklist(Integer page, Integer pagesize) {
        // 1.获取当前登录用户id
        Long userId = UserHolder.getUserId();

        // 2.调用服务提供者api分页查询列表
        IPage<UserInfo> iPage = blackListApi.findBlackList(page, pagesize, userId);

        // 3.封装返回结果
        PageResult pageResult = new PageResult(page, pagesize, (int) iPage.getTotal(), iPage.getRecords());

        return ResponseEntity.ok(pageResult);
    }
    /**
     * 接口名称：黑名单 - 移除
     */
    public ResponseEntity<Object> deleteBlackUser(Long uid) {
        // 1.获取当前登录用户id
        Long userId = UserHolder.getUserId();

        // 2.调用服务提供者api删除黑名单
        blackListApi.deleteBlackUser(userId, uid);

        return ResponseEntity.ok(null);
    }
}
